package com.briup.jz.service.impl;

import com.briup.jz.bean.AccountEmployee;
import com.briup.jz.bean.AccountEmployeeExample;
import com.briup.jz.dao.AccountEmployeeMapper;
import com.briup.jz.service.IAccountEmployeeService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class AccountEmployImpl implements IAccountEmployeeService {
    @Autowired
    private AccountEmployeeMapper accountEmployeeMapper;
    @Override
    public void saveOrUpdate(AccountEmployee accountEmployee) throws CustomerException {
        accountEmployee.setTransferTime(new Date().getTime());
      if (accountEmployee.getId()==null){
          accountEmployeeMapper.insert(accountEmployee);
      }else {
//          否则为更新操作
//          查询条件为根据id查找员工消费信息主键查找
          AccountEmployeeExample example=new AccountEmployeeExample();
//          查找条件设置为查找id值与更新信息中主键id相同的信息
          example.createCriteria().andIdEqualTo(accountEmployee.getId());
//          根据查找信息更新新的信息
          accountEmployeeMapper.updateByExample(accountEmployee,example);
      }
    }

    @Override
    public void delete(Long id) throws CustomerException {
//        根据主键查找用户信息
        AccountEmployee accountEmployee=accountEmployeeMapper.selectByPrimaryKey(id);
//        如果没有找到则抛出异常
        if (accountEmployee==null){
            throw  new CustomerException("没有该员工的这条消费信息");
        }
//       删除该条信息
      accountEmployeeMapper.deleteByPrimaryKey(id);
    }

//    根据员工id查找员工消费信息
    @Override
    public List<AccountEmployee> query(Long id) {
        AccountEmployeeExample example= new AccountEmployeeExample();
//            如果id不为空则按id查询 否则则查询所有
        if(id!=null){
         example.createCriteria().andUserIdEqualTo(id);
        }

        List<AccountEmployee> employees = accountEmployeeMapper.selectByExample(example);
        return employees;
    }
}
